# -*- coding: utf-8 -*-
import wx
import os
from PIL import ImageGrab
# 截图区域背景色
SHOT_BACKGROUND_COLOR = 'white'
SHOT_OPACITY = 30
ENLARGE_RADIO = 120
IMG_PATH = r"D:\\毕设\\截图资料\\"

# 截图区域控制窗口
class ControlArea(wx.Frame):
    position = None
    img = None
    isInit = False
    def __init__(self, parent):
        wx.Frame.__init__(self, parent,
            style = wx.CLIP_CHILDREN | wx.DOUBLE_BORDER
        )
        self.SetTransparent(SHOT_OPACITY)
        self.SetBackgroundColour(SHOT_BACKGROUND_COLOR)

    # 修改截图区域
    def changeArea(self, pos, size):
        self.isInit = True
        # 修正参数，除数是wxpython默认值，被除数是自己电脑的屏幕放大倍数
        recify = ENLARGE_RADIO/96
        self.position = (pos[0]*recify, pos[1]*recify, (pos[0]+size[0])*recify, (pos[1]+size[1])*recify)
        self.SetSize(size)
        self.SetPosition(pos)
        self.Show(True)

    # 取消截图区域
    def cancelArea(self):
        self.isInit = False
        self.Show(False)

    # 开始截图
    def getPhoto(self):
        if 1-self.isInit:
            return
        # 左上右下
        img = ImageGrab.grab(self.position)
        self.saveImage(img)
        return img

    # 图片按序号保存参数
    def saveImage(self, img):
        fileList = os.listdir(IMG_PATH)
        img.save(IMG_PATH + 'img_%d.jpg' % len(fileList))